#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 定时器
    timer = new QTimer(this);

    // 连接timer的信号与槽
    connect(timer, &QTimer::timeout, [=]() {
        currentValue++;
        currentValue %= 60;

        update();
    });

    // 每隔100毫秒产生一个信号timeout
    timer->setInterval(100);
    timer->start();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initCanvas(QPainter &painter)
{
    painter.setRenderHint(QPainter::Antialiasing, true); // 消除锯齿
    painter.setBrush(Qt::black); // 画刷填充色
    painter.drawRect(rect()); // 绘制矩形

    painter.translate(rect().center()); // 移动坐标原点
}

void Widget::drawMiddleCircle(QPainter &painter, int radius)
{
    painter.setPen(QPen(Qt::white, 3)); // 画笔
    painter.drawEllipse(QPoint(0, 0), radius, radius);
}

void Widget::drawScaleLine(QPainter &painter, int radius)
{

    angle = 240 * 1.0 / 60; //总计60个小刻度，每一个小刻度的角度值

    painter.save(); //保存当前坐标
    painter.setPen(QPen(Qt::white, 5));

    painter.rotate(startAngle); //设置起始刻度位置
    for (int i = 0; i <= 60; i++) {
        if ( i >= 40 )
            painter.setPen(QPen(Qt::red, 5)); //第40个刻度后，绘制画笔修改成红色

        if (i % 5 == 0)
            painter.drawLine(radius - 20, 0, radius - 3, 0); //绘制长刻度
        else
            painter.drawLine(radius - 8, 0, radius - 3, 0);//绘制短刻度

        painter.rotate(angle); //绘制完一个刻度旋转一次坐标
    }

    painter.restore(); //恢复坐标
    painter.setPen(QPen(Qt::white, 5));
}

void Widget::drawScaleValue(QPainter &painter, int radius)
{

    QFont textFont("Arial",15); // 设置字体类型和大小
    textFont.setBold(true); // 设置粗体
    painter.setFont(textFont);
    int text_r = radius - 49;

    for (int i = 0; i <= 60; i++) {
        if(i % 5 == 0) {
            if(i >= 40)
                painter.setPen(QPen(Qt::red, 5));

            painter.save(); // 保存当前坐标系
            int delX = qCos((210-angle*i)*M_PI/180) * text_r;
            int delY = qSin(qDegreesToRadians(210-angle*i)) * text_r;

            painter.translate(QPoint(delX,-delY)); //平移坐标系
            painter.rotate(-120+angle*i); //旋转坐标系
            painter.drawText(-25,-25,50,30,Qt::AlignCenter,QString::number(i*4)); //写上刻度值，文字居中
            painter.restore(); //恢复坐标系
        }
    }

    painter.setPen(QPen(Qt::white, 5));
}


void Widget::drawPoint(QPainter &painter, int radius)
{
    //保存当前坐标
    painter.save();
    //设置画刷颜色
    painter.setBrush(Qt::white);
    //设置画笔为无线条
    painter.setPen(Qt::NoPen);
    static const QPointF points[4] = {
        QPointF(0, 0.0),
        QPointF(radius*2.0/3, -1.1),
        QPointF(radius*2.0/3, 1.1),
        QPointF(0, 15.0)
    };
    //坐标选旋转
    painter.rotate(startAngle + angle * currentValue);
    //绘制多边形
    painter.drawPolygon(points, 4);
    //恢复坐标
    painter.restore();
}


void Widget::drawSpeedSector(QPainter &painter, int radius)
{
    //定义矩形区域
    QRect rentangle(-radius, -radius, radius*2, radius*2);
    //设置画笔无线条
    painter.setPen(Qt::NoPen);
    //设置画刷颜色
    painter.setBrush(QColor(255,0,0,80));
    //绘制扇形
    painter.drawPie(rentangle, (360-startAngle)*16, -angle*currentValue*16);
}

void Widget::drawInnerEllipse(QPainter &painter, int radius)
{
    QRadialGradient radial(0,0,radius);
    //中心颜色
    radial.setColorAt(0.0,QColor(255,0,0,200));
    //外围颜色
    radial.setColorAt(1.0,QColor(0,0,0,100));
    //设置画刷渐变色
    painter.setBrush(radial);
    //画圆形
    painter.drawEllipse(QPoint(0,0), radius, radius);
}

void Widget::drawInnerEllipseBlack(QPainter &painter, int radius)
{
    //设置画刷
    painter.setBrush(Qt::black);
    //绘制圆形
    painter.drawEllipse(QPoint(0,0), radius, radius);
}

void Widget::drawCurrentSpeed(QPainter &painter)
{
    painter.setPen(Qt::white);
    //绘制数值
    QFont font("Arial", 28);
    font.setBold(true);
    painter.setFont(font);
    painter.drawText(QRect(-60,-60,120,70),Qt::AlignCenter,QString::number(currentValue*4));
    //绘制单位
    QFont font_u("Arial", 13);
    painter.setFont(font_u);
    painter.drawText(QRect(-60,-60,120,160),Qt::AlignCenter,"km/h");
}

void Widget::drawEllipseOutSkirts(QPainter &painter, int radius)
{
    //设置扇形绘制区域
    QRect outAngle(-radius, -radius, 2*radius, 2*radius);
    painter.setPen(Qt::NoPen);
    //设置渐变色
    QRadialGradient radia(0,0,radius);
    radia.setColorAt(1,QColor(255,0,0,200));
    radia.setColorAt(0.97,QColor(255,0,0,120));
    radia.setColorAt(0.9,QColor(0,0,0,0));
    radia.setColorAt(0,QColor(0,0,0,0));
    painter.setBrush(radia);
    //绘制圆形
    painter.drawPie(outAngle,(360-150)*16,-angle*61*16);
}

void Widget::drawLogo(QPainter &painter, int radius)
{
    //定义Logo绘制区域
    QRect rectLogo(-65,radius*0.38,130,50);
    painter.drawPixmap(rectLogo,QPixmap("./logo2.png"));
}

void Widget::paintEvent(QPaintEvent *event)
{
    // QPainter 的绘制操作必须在 paintEvent 事件中执行
    QPainter painter(this);
    initCanvas(painter);
    drawMiddleCircle(painter, 34);
    drawScaleLine(painter, 224);
    drawScaleValue(painter, 224);
    drawPoint(painter, 195);
    drawSpeedSector(painter, 224);
    drawInnerEllipse(painter,65);
    drawInnerEllipseBlack(painter,44);
    drawCurrentSpeed(painter);
    drawEllipseOutSkirts(painter,240);
}



